//
//  ByBoxListView.m
//  Homept_ios
//
//  Created by pro on 16/8/27.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByBoxListView.h"

@implementation ByBoxListView
{
    NSArray *_titles;
    NSArray *_images;
    
    NSInteger _row;        // 行数
    NSInteger _col;        // 列数
    
    CGFloat _rowHeight;         // 单行高
    CGFloat _colWidht;          // 单列宽
}

- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles images:(NSArray *)images col:(NSInteger)col {
    
    CGFloat row = ceilf(titles.count / (double)col);
    frame.size.height = frame.size.width / col * row;
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        _titles = titles;
        _images = images;
        
        _col = col;
        _row = row;
        
        _colWidht = frame.size.width / _col;
        _rowHeight = _colWidht;
    }
    
    return self;
}
// ratio 是 width : height
- (instancetype)initWithFrame:(CGRect)frame titles:(NSArray *)titles images:(NSArray *)images col:(NSInteger)col ratio:(CGFloat)ratio {
    
    NSArray *items = titles ? titles : images;
    CGFloat row = ceilf(items.count / (double)col);
    frame.size.height = frame.size.width / col / ratio * row;
    
    if (self = [super initWithFrame:frame]) {
        
        self.backgroundColor = [UIColor whiteColor];
        
        _titles = titles;
        _images = images;
        
        _col = col;
        _row = row;
        
        _colWidht = frame.size.width / _col;
        _rowHeight = _colWidht / ratio;
    }
    
    return self;
}

#pragma mark - default attribute
- (CGFloat)imageSize {
    if (!_imageSize) {
        _imageSize = 40.0;
    }
    return _imageSize;
}
- (CGFloat)fontSize {
    if (!_fontSize) {
        _fontSize = 12;
    }
    return _fontSize;
}
- (UIColor *)fontColor {
    if (!_fontColor) {
        _fontColor = [UIColor darkGrayColor];
    }
    return _fontColor;
}
- (CGFloat)contentMargin {
    if (!_contentMargin) {
        _contentMargin = 10;
    }
    return _contentMargin;
}
- (CGFloat)lineWith {
    if (!_lineWith) {
        _lineWith = 1;
    }
    return _lineWith;
}
- (UIColor *)lineColor {
    if (!_lineColor) {
        _lineColor = [UIColor colorWithWhite:0.9 alpha:1];
    }
    return _lineColor;
}

#pragma mark - draw
- (void)drawRect:(CGRect)rect {
    
    [super drawRect:rect];
    
    
    
    
    if (self.showSepLine) {
        for (int i = 0; i < _row + 1; i ++) {            // 两行的话要画1条横线
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path moveToPoint:CGPointMake(0, _rowHeight * i)];
            [path addLineToPoint:CGPointMake(rect.size.width, _rowHeight * i)];
            path.lineWidth = self.lineWith;
            [self.lineColor setStroke];
            [path stroke];
        }
        
        for (int j = 0; j < _col - 1; j ++) {           // 两列的话，只要话一条线
            UIBezierPath *path = [UIBezierPath bezierPath];
            [path moveToPoint:CGPointMake(_colWidht * (j + 1), 0)];
            [path addLineToPoint:CGPointMake(_colWidht * (j + 1), rect.size.height)];
            path.lineWidth = self.lineWith;
            [self.lineColor setStroke];
            [path stroke];
        }
    }

    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSArray *items = _titles ? _titles : _images;
    CGFloat x, y, width, height;
    for (int i = 0; i < items.count; i ++) {
        width = _colWidht - self.lineWith / 2;
        height = _rowHeight - self.lineWith / 2 * 3;
        
        x = (width + self.lineWith) * (i % _col);
        y = self.lineWith + (height + self.lineWith) * (i / _col);
        UIView *basalView = [[UIView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        [self addSubview:basalView];
        basalView.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
        [basalView addGestureRecognizer:tap];
        
        x = 0;
        y = 0;
        width = self.imageSize;
        height = self.imageSize;
        UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(x, y, width, height)];
        imageView.contentMode = UIViewContentModeScaleToFill;
        imageView.image = [UIImage imageNamed:_images[i]];
        [basalView addSubview:imageView];
        
        x = 0;
        y = 0;
        width = [_titles[i] getSizeOfString:self.fontSize].width;
        height = [_titles[i] getSizeOfString:self.fontSize].height;
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
        label.userInteractionEnabled = YES;
        label.font = FONT(self.fontSize);
        label.text = _titles[i];
        label.textColor = self.fontColor;
        label.textAlignment = NSTextAlignmentCenter;
        [basalView addSubview:label];
        
        if (_images == nil) {           // 只有文字
            imageView.hidden = YES;
            label.x = basalView.width / 2;
            label.y = basalView.height / 2;
        } else if (_titles == nil) {    // 只有图片
            label.hidden = YES;
            if (self.contentFill) {
                imageView.width = basalView.width;
                imageView.height = basalView.height;
                imageView.left = 0;
                imageView.top = 0;
            } else {
                imageView.x = basalView.width / 2;
                imageView.y = basalView.height / 2;
            }
        } else {                        // 图文
            CGFloat remain = (basalView.height - (imageView.height + label.height + self.contentMargin)) / 2;
            
            imageView.x = basalView.width / 2;
            imageView.top = remain;
            
            label.x = basalView.width / 2;
            label.top = imageView.bottom + self.contentMargin;
        }
        
        
    }

}


- (void)tapAction:(UITapGestureRecognizer *)tap {

    if (self.indexDidSelect) {
        self.indexDidSelect(tap.view.tag);
    }
}
@end
